package com.jsnkj.basic.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.HomeItemBean
import com.bumptech.glide.Glide


class HomeAdapter(
    val activity: Activity,
    private val homeItemBeans: List<HomeItemBean>,
    val itemClickListener: onItemClickListener
) :
    RecyclerView.Adapter<HomeAdapter.HomeViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.rv_item_home, parent, false)
        return HomeViewHolder(view)
    }

    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val item = homeItemBeans[position]
        Glide.with(activity).load(item.icon).into(holder.ivIcon)
        holder.tvTitle.text = item.title
        holder.tvRemark.text = item.remark

        holder.tvDown.setOnClickListener {
            itemClickListener.onItemCallBack(item, position)
        }
    }

    override fun getItemCount(): Int {
        return homeItemBeans.size
    }

    class HomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivIcon: ImageView
        var tvTitle: TextView
        var tvRemark: TextView
        var tvDown: TextView

        init {
            ivIcon = itemView.findViewById<ImageView>(R.id.iv_icon)
            tvTitle = itemView.findViewById<TextView>(R.id.tv_title)
            tvRemark = itemView.findViewById<TextView>(R.id.tv_remark)
            tvDown = itemView.findViewById<TextView>(R.id.tv_down)
        }
    }


    interface onItemClickListener {
        fun onItemCallBack(bean: HomeItemBean, position: Int)
    }
}